/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.wii.elpro.web.action;

import com.wii.elpro.dao.InqueryDAOInterface;
import com.wii.elpro.dao.hb.BusinessPartnerDAOImpl;
import com.wii.elpro.dao.hb.CabangDAOImpl;
import com.wii.elpro.dao.hb.FinishingDAOImpl;
import com.wii.elpro.dao.hb.InqueryDAOImpl;
import com.wii.elpro.dao.hb.ProductSpecificationImpl;
import com.wii.elpro.dao.hb.ProjectDAOImpl;
import com.wii.elpro.dao.hb.StandartSpecificationImpl;
import com.wii.elpro.dao.hb.UserDAOImpl;
import com.wii.elpro.entity.Inquery;
import com.wii.elpro.entity.User;
import com.wii.elpro.web.form.InquiryNewForm;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author JollkY
 */
public class InquirySaveAction extends Action {
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession sess = request.getSession(true);
        User user = (User)sess.getAttribute("userSession");       
        if(user==null){
            return mapping.findForward("gagalLogin");            
        }  
        String forward;
        InquiryNewForm f = (InquiryNewForm) form;
        InqueryDAOInterface dao = new InqueryDAOImpl();
        Inquery inquiry = new Inquery();
        
        inquiry.setNoQuotation(f.getNoQuotation());
        inquiry.setAlamat(f.getAlamat());
        inquiry.setCabang(new CabangDAOImpl().getCabang(f.getCabang()));
        inquiry.setFinishing(new FinishingDAOImpl().getFinishing(f.getFinishing()));
        inquiry.setKonsultan(new BusinessPartnerDAOImpl().getBusinessPartner(f.getKonsultan()));
        inquiry.setKontraktor(new BusinessPartnerDAOImpl().getBusinessPartner(f.getKontraktor()));
        inquiry.setProductSpec(new ProductSpecificationImpl().getProductSpecification(f.getProductSpec()));
        inquiry.setProject(new ProjectDAOImpl().getProject(f.getProject()));
        inquiry.setSales(new UserDAOImpl().getUser(f.getSales()));
        inquiry.setStandartSpec(new StandartSpecificationImpl().getStandartSpecification(f.getStandartSpec()));
        inquiry.setTanggal(new Date(System.currentTimeMillis()));
        
        ActionMessages messages = new ActionMessages();       
        String fw = request.getParameter("submit");
        try {
            dao.save(inquiry);
            messages.add("customMessage", new ActionMessage("customMessage", "Your data is successfully saved."));
            saveMessages(sess, messages);
            forward = fw;
        } catch (Exception ex) {
            messages.add("message", new ActionMessage("customMessage", "Application is fail to save your data. Please contact my developer."));
            saveMessages(sess, messages);
            ex.printStackTrace();
            forward = "fail";
        }
        return mapping.findForward(forward);
    }
}
